#include "StdAfx.h"
#include "KlassToolbar.h"

const DWORD TOOLBAR_STYLE = 
		/*Window styles:*/  WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | WS_TABSTOP |
		/*Toolbar styles:*/ TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | TBSTYLE_TRANSPARENT | TBSTYLE_LIST | TBSTYLE_CUSTOMERASE |
		/*Common Control styles:*/ CCS_TOP | CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE;


DEFINE_LOGGER(CKlassToolbar)

CriticalSection CKlassToolbar::m_crsecInit;
bool CKlassToolbar::m_bInitialized = false;

TBBUTTON	CKlassToolbar::m_buttons[1];
CManagedImageList CKlassToolbar::m_image_list;
CString		CKlassToolbar::m_strButtonKlass;

CKlassToolbar::CKlassToolbar(void)
{
}

CKlassToolbar::~CKlassToolbar(void)
{
	if (IsWindow()) DestroyWindow();
}

void CKlassToolbar::Init()
{
	LOCK(m_crsecInit);
	if (m_bInitialized)
		return;

	LOG("Initialization...");

	m_strButtonKlass.LoadString(IDS_BUTTON_NAME);

	int icons[] = { IDI_KLASS };
	size_t nIconsCount = sizeof(icons)/sizeof(icons[0]);

	m_image_list.Create(16,16, ILC_COLOR24|ILC_MASK, nIconsCount, 1);

	for (size_t i = 0; i < nIconsCount; ++i)
	{
        CIcon ico;
        if (!ico.LoadIcon(icons[i]))
		{
			LOG("ERROR - Cannot load icon 0x%x", icons[i]);
			continue;
		}

		m_image_list.AddIcon(ico);
	}

	LOG("Initialization buttons...");

	ZeroMemory(m_buttons, sizeof(m_buttons));

	m_buttons[0].idCommand = IDM_KLASS;
	m_buttons[0].fsState = TBSTATE_ENABLED;
	m_buttons[0].fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE;
	m_buttons[0].iString = (INT_PTR)(LPCTSTR)m_strButtonKlass;
	m_buttons[0].iBitmap = 0;

	m_bInitialized = true;
	LOG("Initialization done");
}

bool CKlassToolbar::Create(const CWindow &wndParent)
{
	try
	{
		Init();

		CRect rect(0,0,0,0);
		wndParent.GetClientRect(&rect);
		rect.bottom = rect.top + 22;
		if (rect.right - rect.left < 285)
			rect.right = rect.left + 285;

		LOG("Creating...");
		if (!CWindowImpl<CKlassToolbar, CToolBarCtrl>::Create(wndParent, rect, NULL, TOOLBAR_STYLE))
		{
			LOG("ERROR on creating window!");
			return false;
		}

		LOG("Window has been created 0x%x for 0x%x. Initialization...", (DWORD)m_hWnd, (DWORD)this);
		// buttons with images and text
		SetExtendedStyle(TBSTYLE_EX_MIXEDBUTTONS|TBSTYLE_EX_DRAWDDARROWS);
		// Sets the size of the TBBUTTON structure.
		SetButtonStructSize();
		// Set the maximum number of text rows and bitmap size.
		SetMaxTextRows(16);

		LOG("Initialization image list for 0x%x...", this);
		// Set the toolbar's image
		SetImageList(m_image_list);

		LOG("Adding buttons for 0x%x...", this);
		AddButtons(sizeof(m_buttons)/sizeof(TBBUTTON), m_buttons);

		LOG("Initialization 0x%x done", (DWORD)m_hWnd);

		return true;
	}
	catch(...)
	{
		LOG("EXCEPTION on creation 0x%x", this);
	}
	return false;
}

void CKlassToolbar::SetBrowser(IWebBrowser2* pBrowser)
{
	m_browser = pBrowser;
}

#define TOOLBAR_WIDTH	24

int CKlassToolbar::GetMaxWidth()
{
	int iSize = 0;
	for (int i = 0, count = GetButtonCount(); i < count; ++i)
	{
		TBBUTTON button;
		if (!GetButton(i, &button))
			{ iSize = TOOLBAR_WIDTH; break; }

		if (button.fsState & TBSTATE_HIDDEN)
			continue;

		CRect rect;
		if (GetItemRect(i, &rect))
			iSize += rect.Width();
		else
			{ iSize = TOOLBAR_WIDTH; break; }
	}

	return iSize + 4;
}

void CKlassToolbar::OnButtonPresed()
{
    if (!m_browser || !m_browser.document || !m_browser.document.location || !m_browser.document.location.href)
        return;

    WEBBrowser browser2;
    if (FAILED(browser2.CreateInstance()))
        return;

    int iWindowWidth = 650, iWindowHeight = 400;
    browser2.Left = (::GetSystemMetrics(SM_CXSCREEN)/2)-(iWindowWidth/2);
    browser2.Width = iWindowWidth;
    browser2.Top = (::GetSystemMetrics(SM_CYSCREEN)/2)-(iWindowHeight/2);
    browser2.Height = iWindowHeight;

    browser2.HideAllBars();

    if (SUCCEEDED(browser2.Navigate(::GetString(IDS_SERVICE_URL) + EncodeURL((CString)m_browser.document.location.href), false)))
        browser2.Visible = true;
}

LRESULT CKlassToolbar::OnToolTip(LPNMHDR pnmh)
{
#define lpnmtdi ((LPNMTTDISPINFO)pnmh)

	switch (pnmh->idFrom)
	{
	case IDM_KLASS:
		lpnmtdi->lpszText = (LPTSTR)(LPCTSTR)m_strButtonKlass;
		break;
	}

	return 0;
}
